﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Hp.Vertica.Migration.SqlServer.Test
{
    [TestClass]
    public class DataSourceTest
    {
        [TestMethod]
        public void Connect()
        {
            ServerInstance ds = new ServerInstance("localhost");
            try
            {
                ds.Connect();
            }
            catch
            {
                Assert.Fail("connect via windows credential failed.");
            }

            ServerInstance ds2 = new ServerInstance("localhost", "sa", "sa1qaz2wsx#EDC");
            try
            {
                ds2.Connect();
            }
            catch
            {
                Assert.Fail("connect via SqlServer credential failed.");
            }
        }

        [TestMethod]
        public void Databases()
        {
            ServerInstance ds = new ServerInstance("16.186.76.37", "sa", "sa1qaz!QAZ");
            int dbCnt = ds.Databases.Count;
            ds.ExecuteScript("create database testA");
            ds.Refresh();
            int dbCnt2 = ds.Databases.Count;
            Assert.IsTrue(dbCnt > 0);
            Assert.IsTrue(dbCnt2 > 0);
            Assert.IsTrue(dbCnt2 > dbCnt);
            ds.ExecuteScript("drop database testA");
            ds.Refresh();
            int dbCnt3 = ds.Databases.Count;
            Assert.AreEqual(dbCnt, dbCnt3);
        }

        [TestMethod]
        public void Databases2()
        {
            ServerInstance ds = new ServerInstance("16.186.76.37", "sa", "sa1qaz!QAZ");
            var dbs1 = ds.Databases;
            String newdb = "DB" + Guid.NewGuid().ToString().Replace('-', '_');
            ds.ExecuteScript("create database " + newdb);
            ds.Refresh();
            var dbs2 = ds.Databases;
            foreach (var a in dbs1)
            {
                Assert.IsTrue(dbs2.Contains(a));
            }
            ds.ExecuteScript("drop database " + newdb);

        }
    }
}
